// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.IdPo;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.LoanMonthlyBalance;
import com.fowo.api.model.loan.monthly.balance.LoanMonthlyBalanceItemVo;
import com.fowo.api.model.loan.monthly.balance.LoanMonthlyBalanceSearchParamPo;
import com.fowo.api.model.loan.monthly.balance.LoanMonthlyBalanceVo;
import com.fowo.api.service.LoanMonthlyBalanceService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "货款月结")
@RequestMapping("/api/loan-monthly-balance")
@RestController
public class LoanMonthlyBalanceController {

  @Resource
  private LoanMonthlyBalanceService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<LoanMonthlyBalanceItemVo>> search(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<LoanMonthlyBalanceItemVo>>> searchOptions(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(LoanMonthlyBalanceSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<LoanMonthlyBalanceItemVo>> getPage(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<LoanMonthlyBalanceVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取采购单号相关的编号列表")
  @GetMapping("purchase-order-code/{texts}/ids")
  public R<List<Long>> getIdsByPurchaseOrderCode(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByPurchaseOrderCode(nameList));
  }

  @Operation(summary = "获取编号相关的采购单号列表")
  @GetMapping("purchase-order-code/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getPurchaseOrderCodeByIds(idList));
  }

  @PreAuthorize("@am.canAccess('loanMonthlyBalanceCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody LoanMonthlyBalanceVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('loanMonthlyBalanceEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody LoanMonthlyBalanceVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('loanMonthlyBalanceEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody LoanMonthlyBalanceVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('loanMonthlyBalanceDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('loanMonthlyBalanceDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('loanMonthlyBalanceExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    LoanMonthlyBalanceSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  @PreAuthorize("@am.canAccess('loanMonthlyBalanceFinishRequestPayout')")
  @Operation(summary = "结束请款")
  @PostMapping("finishRequestPayout")
  public R finishRequestPayout(@RequestBody IdPo po) {
    return R.run(() -> service.finishRequestPayout(po.getId()));
  }

  @PreAuthorize("@am.canAccess('loanMonthlyBalanceBatchFinishRequestPayout')")
  @Operation(summary = "结束请款")
  @PostMapping("batchFinishRequestPayout")
  public R batchFinishRequestPayout(@RequestBody IdListPo po) {
    return R.run(() -> service.batchFinishRequestPayout(po.getIds()));
  }

  @Operation(summary = "全部")
  @GetMapping("allmonthly")
  public R<List<LoanMonthlyBalanceItemVo>> allmonthly(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allmonthly(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("allmonthlyPage")
  public R<Page<LoanMonthlyBalanceItemVo>> allmonthlyPage(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allmonthlyPage(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("allmonthlyLastUpdateTime")
  public R<Date> allmonthlyLastUpdateTime(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allmonthlyLastUpdateTime(search));
  }

  @Operation(summary = "待申请")
  @GetMapping("wait-apply")
  public R<List<LoanMonthlyBalanceItemVo>> waitApply(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitApply(search));
  }

  @Operation(summary = "待申请分页")
  @GetMapping("wait-applyPage")
  public R<Page<LoanMonthlyBalanceItemVo>> waitApplyPage(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitApplyPage(search));
  }

  @Operation(summary = "待申请最后更新时间")
  @GetMapping("wait-applyLastUpdateTime")
  public R<Date> waitApplyLastUpdateTime(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitApplyLastUpdateTime(search));
  }

  @Operation(summary = "全额申请")
  @GetMapping("infull-apply")
  public R<List<LoanMonthlyBalanceItemVo>> infullApply(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.infullApply(search));
  }

  @Operation(summary = "全额申请分页")
  @GetMapping("infull-applyPage")
  public R<Page<LoanMonthlyBalanceItemVo>> infullApplyPage(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.infullApplyPage(search));
  }

  @Operation(summary = "全额申请最后更新时间")
  @GetMapping("infull-applyLastUpdateTime")
  public R<Date> infullApplyLastUpdateTime(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.infullApplyLastUpdateTime(search));
  }

  @Operation(summary = "已付清")
  @GetMapping("paid-off")
  public R<List<LoanMonthlyBalanceItemVo>> paidOff(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.paidOff(search));
  }

  @Operation(summary = "已付清分页")
  @GetMapping("paid-offPage")
  public R<Page<LoanMonthlyBalanceItemVo>> paidOffPage(
    LoanMonthlyBalanceSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.paidOffPage(search));
  }

  @Operation(summary = "已付清最后更新时间")
  @GetMapping("paid-offLastUpdateTime")
  public R<Date> paidOffLastUpdateTime(LoanMonthlyBalanceSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.paidOffLastUpdateTime(search));
  }
}
